﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Cole.Entities;
using Cole.Service;

namespace Cole.GUI.Grado_Seccion
{
    public partial class FrmGrado : Form
    {
        private IGradoBl gradoBl;
        public FrmGrado()
        {
            InitializeComponent();
            if (gradoBl == null)
            {
                gradoBl = new GradoBl();
            }
        }

        private void FrmGrado_Load(object sender, EventArgs e)
        {
            gradoBindingSource.DataSource = gradoBl.GetAllGrado();
            gradoBindingSource.Insert(0, new Grado() { ID_Grado = 0, Nombre_Grado = "[-- TODOS --]" });
            cbGrado.SelectedIndex = 0;
            
            LoadData();
            
        }

        private void LoadData()
        {
            var id_grado = Int32.Parse(cbGrado.SelectedValue.ToString());
            grado_SeccionBindingSource.DataSource = gradoBl.GetGrado_Seccion(id_grado);
            

        }

        private void cbGrado_SelectedValueChanged(object sender, EventArgs e)
        {
            //if (cbGrado.SelectedValue != null)
            //{
            //    gradoBindingSource.ResetBindings(true);
            //}
        }

        private void cbGrado_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbGrado.SelectedValue != null)
            {
                LoadData();
            }
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            var grado = grado_SeccionBindingSource.Current as GradoSeccion;
            if (grado != null)
            {
                var frmEditar = new FrmEditarGrado_Seccion();
                frmEditar.Recibir(grado);
                frmEditar.ShowDialog();
            }
            
        }

        private void btnNuevoGrado_Click(object sender, EventArgs e)
        {
           var frmNuevo = new FrmNuevoGrado();
            frmNuevo.nuevo();
            frmNuevo.ShowDialog();

        }

        private void btnNuevaSeccion_Click(object sender, EventArgs e)
        {
            var frmNuevo = new FrmNuevaSeccion();
            frmNuevo.nuevo();
            frmNuevo.ShowDialog();
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            try
            {
                var grado = grado_SeccionBindingSource.Current as GradoSeccion;
                if (grado != null)
                {
                    var rpt = MessageBox.Show(this, "Desea eliminar la Seccion : " + grado.Nombre_Seccion+ "  del Grado :  " + grado.Nombre_Grado, "Mensaje",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question);
                    if (rpt == DialogResult.Yes)
                    {
                        grado_SeccionBindingSource.RemoveCurrent();
                        gradoBl.eliminarSeccion(grado.ID);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
